﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QualityRpt.Service;
using System.Text;
using System.IO;
using QualityRpt.Utils;

namespace QualityRPT
{
    public partial class StatJobResult : System.Web.UI.Page
    {
        public string jobDesc = string.Empty;
        public bool hasResult = true;
        DateTime jobDataStart;
        DateTime jobDataEnd;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string id = Request.Params["id"];
                int job_id = Convert.ToInt32(id);
                DataRow jobRow = ServiceInfoService.Get(job_id);
                if (jobRow != null)
                {
                    DateTime jobDataStart = Convert.ToDateTime(jobRow["DATA_START_TIME"]);
                    DateTime jobDataEnd = Convert.ToDateTime(jobRow["DATA_END_TIME"]);
                    jobDesc = "任务名称:" + jobRow["JOB_NAME"].ToString() + ";统计时间段:" + jobDataStart.ToString("yyyy-MM-dd HH:mm") + "-" + jobDataEnd.ToString("yyyy-MM-dd HH:mm");
                }
                DataTable rTable = RPTResultService.GetByJob(job_id);
                int temp_id = Convert.ToInt32(jobRow["TEMP_ID"]);
                resultTable = RPTResultService.FormatResult(temp_id, rTable);
                if (resultTable.Rows.Count > 0)
                {
                    string strExport = Request.Params["export"];
                    if (strExport != null && strExport == "1")
                    {
                        //for (int i = 0; i < resultTable.Columns.Count; i++)
                        //{
                        //    resultTable.Columns[i].Caption = AppTools.GetFiledText(resultTable.Columns[i].ColumnName);
                        //}
                        //MemoryStream xlms = AppTools.RenderToExcel(resultTable);
                        //string file_name = jobRow["JOB_NAME"].ToString() + ".xls";
                        //AppTools.RenderToBrowser(xlms, Context, file_name);
                        string checkDate = jobDataStart.ToString("yyyy-MM-dd HH:mm") + "-" + jobDataEnd.ToString("yyyy-MM-dd HH:mm");
                        string reportFile = Server.MapPath("/ReportTemplate/Service.xls");
                        string file_name = jobRow["JOB_NAME"].ToString() + "报表" + ".xls";
                        string headname = jobRow["JOB_NAME"].ToString() + "报表"; ;
                        SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(reportFile);
                        Common.Common.ServiceExportData(Response, resultTable, workbook, 0, file_name, checkDate, headname);
                    }
                }
                else {
                    hasResult = false;
                    string strExport = Request.Params["export"];
                    if (strExport != null && strExport == "1")
                    {
                        Response.Write("<script>alert('您查询的数据为空.');</script>");
                    }
                }
            }
        }
        DataTable resultTable = null;
        public string GenTableHead()
        {
            StringBuilder sbText = new StringBuilder();
            for (int i = 0; i < resultTable.Columns.Count; i++)
            {
                sbText.Append("<th>"+AppTools.GetFiledText(resultTable.Columns[i].ColumnName)+"</th>");
            }
            return sbText.ToString();
        }
        public string GenTableBody()
        {
            StringBuilder sbText = new StringBuilder();
            for (int i = 0; i < resultTable.Rows.Count; i++)
            {
                sbText.Append("<tr>");
                DataRow curRow = resultTable.Rows[i];
                for (int j = 0; j < resultTable.Columns.Count; j++)
                {
                    sbText.Append("<td>");
                    DataColumn curCol = resultTable.Columns[j];
                    if (curRow[j] != DBNull.Value)
                    {
                        if (curCol.DataType == typeof(decimal))
                        {
                            decimal decRate = Convert.ToDecimal(curRow[j]);
                            sbText.Append(StringUtils.FormatRatio(decRate) + "%");
                        }
                        else
                        {
                            sbText.Append(curRow[j].ToString());
                        }
                    }
                    else {
                        if(curCol.DataType == typeof(decimal) || curCol.DataType == typeof(int)){
                            sbText.Append("0");
                        }
                    }
                    sbText.Append("</td>");
                }
                sbText.Append("</tr>");
            }
            return sbText.ToString();
        }
    }
}